﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model;

namespace Backend.JYD.Business
{
    public class CodeManager
    {
        static readonly IDAL.ICodeInfo idal = Factory.DALFactory.CreateEFDAL<IDAL.ICodeInfo>("DAL.CodeInfoDAL");

        #region List<CodeInfo>

        static List<CodeInfo> _allCodeInfo = null;
        static object _lockPadCodeInfo = new object();
        static List<CodeInfo> AllCodeInfo
        {
            get
            {
                if (_allCodeInfo == null)
                {
                    lock (_lockPadCodeType)
                    {
                        if (_allCodeInfo == null)
                        {
                            var query = idal.GetCodeInfo();
                            if (query != null)
                            {
                                _allCodeInfo = query.ToList();
                            }
                        }
                    }
                }

                return _allCodeInfo;
            }
        }

        #endregion

        #region List<CodeType>

        static List<CodeType> _allCodeType = null;
        static object _lockPadCodeType = new object();
        static List<CodeType> AllCodeType
        {
            get
            {
                if (_allCodeType == null)
                {
                    lock (_lockPadCodeType)
                    {
                        if (_allCodeType == null)
                        {
                            var query = idal.GetCodeType();
                            if (query != null)
                            {
                                _allCodeType = query.ToList();
                            }
                        }
                    }
                }
                return _allCodeType;
            }
        }

        #endregion

        #region 函数
        
        public static List<CodeInfo> GetCodeInfoByTypeCode(string type)
        {
            var list = CodeManager.AllCodeInfo;
            if (list == null) return null;

            return list.FindAll(p => p.TypeCode == type).OrderBy(p=>p.Sort).ToList();
        }

        public static string GetCodeNameByTypeCode(string type, string code)
        {
            var codeInfos = CodeManager.GetCodeInfoByTypeCode(type);
            if (codeInfos == null) return string.Empty;

            var codeInfo = codeInfos.Find(p => p.Code == code);
            return codeInfo == null ? string.Empty : codeInfo.Name;
        }

        #endregion

        #region CodeType_Code

        /// <summary>
        /// 证件类型:1-身份证; 2-军官证
        /// </summary>
        public const string PaperType = "PaperType";
        /// <summary>
        /// UserManager UserType { 普通客户 = 1, 合作机构 = 2, VIP投资客户 = 3, 兼职人员 = 4 }
        /// </summary>
        public const string UserType = "UserType";

        /// <summary>
        /// CustManager CustChanelType{ 线上营销 = 1, 线上推荐 = 2, 线下营销 = 3, 线下推荐 = 4 }
        /// </summary>
        public const string CustChanelType = "CustChanelType";

        /// <summary>
        /// 1-是，0-否
        /// </summary>
        public const string YesOrNo = "YesOrNo";
        /// <summary>
        /// 计息方式
        /// </summary>
        public const string RepaymentType = "RepaymentType";

        #endregion

    }
}
